package com.zhaoyang.servlet;

import com.zhaoyang.dao.AdminDao;
import com.zhaoyang.dao.StudentDao;
import com.zhaoyang.dao.TeacherDao;
import com.zhaoyang.model.Admin;
import com.zhaoyang.model.Student;
import com.zhaoyang.model.Teacher;
import com.zhaoyang.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 赵扬
 * @time 2020/4/28 13:03
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = -5870852067427524781L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        response.setContentType("text/html; charset=UTF-8");
        if ("logout".equals(method)) {
            logout(request, response);
            return;
        }
        // username=xie&password=1234&vcode=k3b6&type=3
        String vcode = request.getParameter("vcode");
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        int type = Integer.parseInt(request.getParameter("type"));

        String loginCpacha = request.getSession().getAttribute("loginCapcha").toString();

        if (StringUtil.isEmpty(vcode)) {
            response.getWriter().write("vcodeError");
            return;
        }

        // 验证码忽略大小写
        if (!vcode.toUpperCase().equals(loginCpacha.toUpperCase())) {
            response.getWriter().write("vcodeError");
            return;
        }
        //验证码验证通过，对比用户名密码是否正确
        String loginStatus = "loginError";

        switch (type) {
            case 1: { // 管理员
                AdminDao adminDao = new AdminDao();
                Admin admin = adminDao.login(name, password);
                if (admin == null) {
                    response.getWriter().write("loginError");
                    return;
                }
                HttpSession session = request.getSession();
                session.setAttribute("user", admin);
                session.setAttribute("userType", type);
                loginStatus = "loginSuccess";
                break;
            }
            case 2: { // 学生
                StudentDao studentDao = new StudentDao();
                Student student = studentDao.login(name, password);
                if (student == null) {
                    response.getWriter().write("loginError");
                    return;
                }
                // 设置session session的实现依赖于cookie
                HttpSession session = request.getSession();
                session.setAttribute("user", student);
                session.setAttribute("userType", type);
                loginStatus = "loginSuccess";
                break;
            }
            case 3: {
                TeacherDao teahcerDao = new TeacherDao();
                Teacher teacher = teahcerDao.login(name, password);
                if (teacher == null) {
                    response.getWriter().write("loginError");
                    return;
                }
                HttpSession session = request.getSession();
                session.setAttribute("user", teacher);
                session.setAttribute("userType", type);
                loginStatus = "loginSuccess";
                break;
            }
            default:
                break;
        }
        response.getWriter().write(loginStatus);

    }

    /**
     * 注销账号
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 从session中删除用户
        request.getSession().removeAttribute("user");
        request.getSession().removeAttribute("userType");
        response.sendRedirect("index.jsp"); // 重定向首页
    }
}
